package com.xunk.system.controller;

import com.xunk.common.core.utils.StringUtils;
import com.xunk.common.core.utils.poi.ExcelUtil;
import com.xunk.common.core.web.controller.BaseController;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.common.core.web.page.TableDataInfo;
import com.xunk.common.log.annotation.Log;
import com.xunk.common.log.enums.BusinessType;
import com.xunk.system.domain.SysMatterLog;
import com.xunk.system.domain.SysTenement;
import com.xunk.system.domain.SysTenementDetail;
import com.xunk.system.domain.SysVisitorDetailLog;
import com.xunk.system.service.ISysTenementService;
import com.xunk.system.vo.VisitLogReqParamVO;
import com.xunk.system.vo.hsh.HshMatterHistoryReqVO;
import com.xunk.system.vo.hsh.HshMatterSubmitVO;
import com.xunk.system.vo.hsh.HshVisitorDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.xunk.common.core.web.domain.AjaxResult.DATA_TAG;

/**
 * 社区物业Controller
 * 
 * @author ruoyi
 * @date 2021-03-18
 */
@RestController
@RequestMapping("/tenement")
public class SysTenementController extends BaseController
{
    @Autowired
    private ISysTenementService sysTenementService;

    /**
     * 查询社区物业列表
     */
    @PreAuthorize("@ss.hasPermi('buz:tenement:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTenementDetail sysTenement)
    {
        startPage();
        List<SysTenementDetail> list = sysTenementService.selectSysTenementList(sysTenement);
        return getDataTable(list);
    }

    /**
     * 获取社区物业详细信息
     */
    @PreAuthorize("@ss.hasPermi('buz:tenement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysTenementService.selectSysTenementById(id));
    }

    @Log(title = "社区物业", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('buz:tenement:export')")
    @PostMapping("/export")
    public void exportConsume(HttpServletResponse response, SysTenementDetail sysTenement) throws IOException {
        List<SysTenementDetail> list = sysTenementService.selectSysTenementList(sysTenement);
        ExcelUtil<SysTenementDetail> util = new ExcelUtil<>(SysTenementDetail.class);
        util.exportExcel(response, list, "社区数据");
    }

    /**
     * 新增社区物业
     */
    @PreAuthorize("@ss.hasPermi('buz:tenement:add')")
    @Log(title = "社区物业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTenement sysTenement)
    {
        return toAjax(sysTenementService.insertSysTenement(sysTenement));
    }

    /**
     * 修改社区物业
     */
    @PreAuthorize("@ss.hasPermi('buz:tenement:edit')")
    @Log(title = "社区物业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTenement sysTenement)
    {
        return toAjax(sysTenementService.updateSysTenement(sysTenement));
    }

    /**
     * 删除社区物业
     */
    @PreAuthorize("@ss.hasPermi('buz:tenement:remove')")
    @Log(title = "社区物业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysTenementService.deleteSysTenementByIds(ids));
    }

    /**
     *  登录物业系统
     * @param homeId    家庭id
     * @return  结果
     */
    @Log(title = "登录物业", businessType = BusinessType.OTHER)
    @PostMapping("/login")
    public AjaxResult loginTenement(Long homeId){
        sysTenementService.loginTenement(homeId);
        return AjaxResult.success();
    }

    /**
     *  获取社区通告 - 支持分页查询
     * @param homeId    家庭id
     * @param current   当前页数
     * @param size      当前页大小
     * @return  结果
     */
    @GetMapping("/notice")
    public AjaxResult getTenementNotice(Long homeId,Integer current,Integer size){
        return AjaxResult.success(sysTenementService.getTenementNotice(homeId, current,size));
    }

    /**
     *  获取对应的家庭列表 - 用于生成访客记录时选择地址信息
     * @param homeId    家庭id
     * @return
     */
    @GetMapping("/homes")
    public AjaxResult getHomes(Long homeId){
        return AjaxResult.success(sysTenementService.getHouses(homeId));
    }

    /**
     *  获取账单信息
     * @param homeId 家庭id
     * @return
     */
    @GetMapping("/bill")
    public AjaxResult getHomeBills(Long homeId,@RequestParam(required = false,defaultValue = "1") Integer pageNum){
        return AjaxResult.success(sysTenementService.getFeeBill(homeId, pageNum));
    }

    /**
     *  获取保修类型
     * @return 响应报应类型
     */
    @GetMapping("/matter")
    public AjaxResult getMatterType(){
        return AjaxResult.success(DATA_TAG,sysTenementService.getMatterType());
    }

    @PostMapping("/code")
    public AjaxResult createVisitorCode(@RequestBody HshVisitorDetailVO vo){
        return AjaxResult.success(DATA_TAG,sysTenementService.createVisitorCode(vo));
    }

    /**
     *  上报报事报修
     * @param vo    内容
     * @return
     */
    @PostMapping ("/matter")
    public AjaxResult submitMatter(@RequestBody HshMatterSubmitVO vo){
        sysTenementService.doSubmitMatter(vo);
        return AjaxResult.success();
    }

    /**
     *  上报报事报修
     * @param homeId    家庭id
     * @param id    保修id
     * @return
     */
    @GetMapping ("/detail")
    public AjaxResult submitMatter(Long homeId,String id){
        return AjaxResult.success(sysTenementService.getMatterDetail(homeId,id));
    }

    /**
     *  获取报事报修历史记录
     * @return
     */
    @GetMapping("/history")
    public AjaxResult getMatterLog(Long homeId,String compositeStatus,Integer current,Integer size){
        HshMatterHistoryReqVO reqVO = new HshMatterHistoryReqVO();
        if (StringUtils.isNotEmpty(compositeStatus)){
            reqVO.setCompositeStatus(compositeStatus);
        }
        if (current != null){
            reqVO.setCurrent(current);
        }
        if (size != null){
            reqVO.setSize(size);
        }
        return AjaxResult.success(sysTenementService.getMatterHis(homeId,reqVO));
    }

    @GetMapping("/visit/log")
    public TableDataInfo getVisitLog(VisitLogReqParamVO paramVO){
        startPage();
        List<SysVisitorDetailLog> list = sysTenementService.getVisitLog(paramVO);
        return getDataTable(list);
    }

    @Log(title = "社区物业", businessType = BusinessType.EXPORT)
    @PostMapping("/visit/export")
    public void exportVisitorLog(HttpServletResponse response, VisitLogReqParamVO paramVO) throws IOException {
        List<SysVisitorDetailLog> list = sysTenementService.getVisitLog(paramVO);
        ExcelUtil<SysVisitorDetailLog> util = new ExcelUtil<>(SysVisitorDetailLog.class);
        util.exportExcel(response, list, "访客数据");
    }

    @DeleteMapping("/visit/delete")
    public AjaxResult deleteVisitLogs(Long[] ids){
        sysTenementService.deleteVisitLog(ids);
        return AjaxResult.success();
    }

    @GetMapping("/matter/log")
    public TableDataInfo getMatterLog(SysMatterLog log){
        startPage();
        return getDataTable(sysTenementService.getMatterLog(log));
    }

    @Log(title = "社区物业", businessType = BusinessType.EXPORT)
    @PostMapping("/matter/export")
    public void exportMatterLog(HttpServletResponse response, SysMatterLog log) throws IOException {
        List<SysMatterLog> list = sysTenementService.getMatterLog(log);
        ExcelUtil<SysMatterLog> util = new ExcelUtil<>(SysMatterLog.class);
        util.exportExcel(response, list, "报事报修数据");
    }


    @DeleteMapping("/matter/delete")
    public AjaxResult deleteMatterLog(Long[] ids){
        sysTenementService.deleteMatterLog(ids);
        return AjaxResult.success();
    }

    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("files") MultipartFile[] files) throws IOException {
        return AjaxResult.success(DATA_TAG,sysTenementService.uploadFile(files));
    }
}
